<?php

namespace App\Service\OpenShop\FeishuMini;

use App\Models\Common\ConfigModel;
use App\Models\Kms\User\KmsUser;
use App\Models\OpenShop\InvestmentPromotion\OpenShopAddressModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Service\BaseService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class SiteLocationFeiShuMiniService extends BaseService
{

    /**
     * 根据订单号获得是否是重点城市
     * @param string $orderNo
     * @return bool
     */
    public static function getKeyPointByOrderNo(string $orderNo): bool
    {
        if (!mb_strlen($orderNo)) {
            return false;
        }
        $openShopSiteLocalAddress = (string)OpenShopSiteLocationModel::query()
            ->whereIn('source', [OpenShopSiteLocationModel::SOURCE_OPEN_SHOP, OpenShopSiteLocationModel::SOURCE_HOUSING])
            ->where('orderNo', $orderNo)
            ->value('cityArea');

        //获得配置的重点城市
        $keyPointCity = ProgrammingService::hotCity()['hotCity'] ?? [];

        $judgeFunction = function ($address) use ($keyPointCity){
            foreach ($keyPointCity as $city) {
                if (strpos($address, $city) !== false) {
                    return true;
                }
            }
            return false;
        };

        if (mb_strlen($openShopSiteLocalAddress)) {
            $keyPoint = $judgeFunction($openShopSiteLocalAddress);
        } else {
            $address  = (string)OpenShopAddressModel::query()->where('orderNo', $orderNo)->value('addressPrefix');
            $keyPoint = $judgeFunction($address);
        }

        return $keyPoint;
    }


    /**
     * 获得配置的选址指派配置
     * @return Builder|Model|object|null
     */
    public static function getAssignHandlerConfig()
    {
        $rmsUserId = (int)ConfigModel::query()
            ->where('module', 'openShop')
            ->where('name', 'assignHandler')
            ->value('value');
        return KmsUser::query()->where('rmsUserId', $rmsUserId)->first();
    }
}
